package com.wc.alorithm_blue_bridge._动态规划.线性DP.最大阶梯;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/22 13:00
 * @description https://www.lanqiao.cn/problems/17147/learning/?problem_list_id=33&page=1
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    static int[][] g = new int[N][N], g1 = new int[N][N];
    static int[][] f1 = new int[N][N], f2 = new int[N][N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                g[i][j] = sc.nextInt();
                f1[i][j] = f2[i][j] = 1;
            }
        }
        int res = 1;
        for (int st = 1; st <= 4; st++) {
            for (int i = 1; i <= n; i++) {
                int x = 1, y = i;
                while (x <= n && y >= 1) {
                    if (x - 1 >= 1 && y + 1 <= n) {
                        if (g[x][y] == g[x - 1][y + 1]) {
                            f1[x][y] = f1[x - 1][y + 1] + 1;
                        }
                    }
                    if ((x - 1 >= 1)) {
                        if (g[x][y] == g[x - 1][y]) {
                            if (f2[x - 1][y] < f1[x][y]) {
                                f2[x][y] = f2[x - 1][y] + 1;
                            } else {
                                f2[x][y] = f1[x][y];
                            }
                            res = Math.max(f2[x][y], res);
                        }
                    }
                    x++;
                    y--;
                }
            }
            for (int i = 2; i <= n; i++) {
                int x = i, y = n;
                while (x <= n && y >= 1) {
                    if (x - 1 >= 1 && y + 1 <= n) {
                        if (g[x][y] == g[x - 1][y + 1]) {
                            f1[x][y] = f1[x - 1][y + 1] + 1;
                        }
                    }
                    if ((x - 1 >= 1)) {
                        if (g[x][y] == g[x - 1][y]) {
                            if (f2[x - 1][y] < f1[x][y]) {
                                f2[x][y] = f2[x - 1][y] + 1;
                            } else {
                                f2[x][y] = f1[x][y];
                            }
                            res = Math.max(f2[x][y], res);
                        }
                    }
                    x++;
                    y--;
                }
            }
            roll(g, g1);
            int[][] tmp = g;
            g = g1;
            g1 = tmp;
        }

        out.println(res);
        out.flush();
    }

    static void roll(int[][] a, int[][] b) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                b[j][n - i + 1] = a[i][j];
                f1[i][j] = f2[i][j] = 1;
            }
        }
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}